﻿using Microsoft.Vbe.Interop;
using System.CodeDom;
using System.Collections.Generic;
using System.Diagnostics;
using VBACodeingTool.Module;

namespace VBACodeingTool
{
    public class CodeLoaderHelper
    {
        private static LoaderModule LoaderModule(string codeContent, string parentName, ParentType parentType)
        {
            return new LoaderModule(codeContent, parentName, parentType);
        }

        public static ProjectCodeModule GetCurrentModuleProjectData()
        {
            string codeContent = VBEHelper.GetMultiLineString(1, VBEHelper.GetCurrentCodeModule().CountOfLines);
            string parentName = VBEHelper.GetCurrentCodePane().CodeModule.Name;
            VBComponent vBComponent = VBEHelper.GetCurrentComponent();
            if (ConvertParentType(vBComponent.Type) != ParentType._NoType)
            {
                return new ProjectCodeModule(parentName, LoaderModule(codeContent, parentName, ConvertParentType(vBComponent.Type)).ParseCode(VBEHelper.GetCurrentSubContent()));
            }
            else
            {
                return null;
            }
        }

        public static Dictionary<string, List<ProjectCodeData>> GetAllModules()
        {
            Dictionary<string, List<ProjectCodeData>> modules = new Dictionary<string, List<ProjectCodeData>>();
            VBProject vBProject = VBEHelper.GetCurrentVBProject();
            foreach (VBComponent item in vBProject.VBComponents)
            {
                if (ConvertParentType(item.Type) != ParentType._NoType)
                {
                    if (item.CodeModule.CountOfLines > 1)
                    {
                        string codeContent = item.CodeModule.Lines[1, item.CodeModule.CountOfLines];
                        string parentName = item.CodeModule.Name;
                        modules.Add(parentName, LoaderModule(codeContent, parentName, ConvertParentType(item.Type)).ParseCode());
                    }
                }
            }

            return modules;
        }

        private static ProjectCodeModule PackProjectModule(VBComponent item, ParentType parentType)
        {
            string codeContent = item.CodeModule.Lines[1, item.CodeModule.CountOfLines];
            Debug.WriteLine($"fileName:{item.Name}content:{codeContent}");
            string parentName = item.CodeModule.Name;
            return new ProjectCodeModule(parentName, LoaderModule(codeContent, parentName, parentType).ParseCode());
        }

        public static ParentType ConvertParentType(vbext_ComponentType vbext_Component)
        {
            switch (vbext_Component)
            {
                case vbext_ComponentType.vbext_ct_StdModule:
                    return ParentType._Module;

                case vbext_ComponentType.vbext_ct_ClassModule:
                    return ParentType._Class;

                case vbext_ComponentType.vbext_ct_Document:
                    return ParentType._Document;

                default: return ParentType._NoType;
            }
        }

        public static CodeDataManager CreateCodeManager()
        {
            return new CodeDataManager(GetAllModules());
        }
    }
}